#include "Mob.h"
#include "Cell.h"

#include <Time.h>
#include <stdlib.h>

  // has Direction functions in it
Direction cellToDirection( Cell c, Mob* m ) {
    if( !c.isNull() ) return NONE;

    int x1 = m->getX(), y1 = m->getY();
    int x2 = c.getX(),  y2 = c.getY();

    int endX = x2 - x1, endY = y2 - y1;

    switch( endX ) {
        case( -1 ):
            switch( endY ) {
                case( -1 ): return NORTHWEST; break;
                case( 0  ): return WEST;      break;
                case( 1  ): return SOUTHWEST; break;
                default:    return NONE;      break;
            }
            break;
        case( 0 ):
            switch( endY ) {
                case( -1 ): return NORTH;     break;
                case( 1  ): return SOUTH;     break;
                default:    return NONE;      break;
            }
            break;
        case( 1 ):
            switch( endY ) {
                case( -1 ): return NORTHEAST; break;
                case( 0  ): return EAST;      break;
                case( 1  ): return SOUTHEAST; break;
                default:    return NONE;      break;
            }
            break;
        default:            return NONE;      break;
    }

    return NONE;
}
/*      case     NORTH :                y -= distance; break;
        case NORTHEAST : x += distance; y -= distance; break;
        case      EAST : x += distance;                break;
        case SOUTHEAST : x += distance; y += distance; break;
        case     SOUTH :                y += distance; break;
        case SOUTHWEST : x -= distance; y += distance; break;
        case      WEST : x -= distance;                break;
        case NORTHWEST : x -= distance; y -= distance; break;
            */

Direction randDirection(){
    //srand( time(NULL) );
    int i = rand() % 8;
    switch( i ){
        case 0 : return     NORTH; break;
        case 1 : return NORTHEAST; break;
        case 2 : return      EAST; break;
        case 3 : return SOUTHEAST; break;
        case 4 : return     SOUTH; break;
        case 5 : return SOUTHWEST; break;
        case 6 : return      WEST; break;
        case 7 : return NORTHWEST; break;
        default: return     NORTH; break;
    }
}

Direction invertDirection( Direction d ){
    switch( d ){
        case     NORTH : return SOUTH;     break;
        case NORTHEAST : return SOUTHWEST; break;
        case      EAST : return WEST;      break;
        case SOUTHEAST : return NORTHWEST; break;
        case     SOUTH : return NORTH;     break;
        case SOUTHWEST : return NORTHEAST; break;
        case      WEST : return EAST;      break;
        case NORTHWEST : return SOUTHEAST; break;
        default: return NONE; break;
    }
}

void operator++( Direction& d ){
    d = Direction( int (d) + 1 );
}

void Mob::damage( int value ){
    lifes -= value;
}

void Mob::earnLevel( int i ) {
    if( level != LEVELCAP ) {
        level += i;
        if( level > LEVELCAP ) {
            level = LEVELCAP;
        }
    }
}

